/****************************************************************************
 * Copyright (c) 2010 Torkild U. Resheim
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Torkild U. Resheim  - initial API and implementation
 *****************************************************************************/
package no.resheim.buildmonitor.ui.views;

import java.util.Collection;
import java.util.Observable;
import java.util.Observer;

import no.resheim.buildmonitor.core.BuildmonitorCore.ServerList;
import no.resheim.buildmonitor.core.data.Server;
import no.resheim.buildmonitor.ui.BuildmonitorUI;
import no.resheim.buildmonitor.ui.preferences.PreferenceConstants;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;

public class JobViewContentProvider implements ITreeContentProvider, Observer,
		IStructuredContentProvider {

	Viewer viewer;
	ServerList input;

	public JobViewContentProvider() {
		store = BuildmonitorUI.getDefault().getPreferenceStore();
		// Make sure we refresh when preferences have changed.
		store.addPropertyChangeListener(new IPropertyChangeListener() {

			@Override
			public void propertyChange(PropertyChangeEvent event) {
				viewer.getControl().getDisplay().asyncExec(new Runnable() {
					@Override
					public void run() {
						viewer.refresh();
					}
				});
			}
		});
	}

	@Override
	public Object[] getElements(Object inputElement) {
		if (input == null) {
			return new Object[0];
		}
		Collection<?> values = input.getServers();
		for (Object object : values) {
			if (object instanceof Observable) {
				((Observable) object).addObserver(this);
			}
		}
		return values.toArray();
	}

	@Override
	public void dispose() {
		if (input != null) {
			input.deleteObserver(this);
		}
	}

	@Override
	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		this.viewer = viewer;
		if (newInput instanceof ServerList) {
			input = (ServerList) newInput;
			input.addObserver(this);
		}
	}

	IPreferenceStore store;

	/**
	 * Updates the view with the latest information
	 */
	public void update(final Observable o, final Object arg) {
		// The viewer may have been disposed
		if (!viewer.getControl().isDisposed()) {
			viewer.getControl().getDisplay().asyncExec(new Runnable() {
				public void run() {
					// Update the server presentation
					((TreeViewer) viewer).refresh(o);
				}
			});
		}
	}

	@Override
	public Object[] getChildren(Object parentElement) {
		if (parentElement instanceof Server) {
			return ((Server) parentElement)
					.getFilteredJobs(
							store.getBoolean(PreferenceConstants.P_IGNORE_DEACTIVATE_JOBS))
					.toArray();
		}
		return new Object[0];
	}

	@Override
	public Object getParent(Object element) {
		return null;
	}

	@Override
	public boolean hasChildren(Object element) {
		if (element instanceof Server) {
			return ((Server) element)
					.getFilteredJobs(
							store.getBoolean(PreferenceConstants.P_IGNORE_DEACTIVATE_JOBS))
					.size() > 0;
		}
		return false;
	}

}
